package pl.matt.media.utils;

import java.util.Arrays;
import java.util.Collection;

import pl.matt.model.Rectangle;

/**
 * @author mateusz
 *
 */

public class RectangleUtils {

	public static Rectangle getAverageRectangle(Rectangle... rectangles) {
		return getAverageRectangle(Arrays.asList(rectangles));
	}
	
	public static Rectangle getAverageRectangle(Collection<Rectangle> rectangles) {
		if(rectangles == null || rectangles.size() == 0) {
			return null;
		}
		int size = rectangles.size();
		if (size == 1) {
			return rectangles.iterator().next();
		}
		int x = 0;
		int y = 0;
		int w = 0;
		int h = 0;
		for (Rectangle rectangle : rectangles) {
			x += rectangle.getX();
			y += rectangle.getY();
			w += rectangle.getWidth();
			h += rectangle.getHeight();
		}
		Rectangle out = new Rectangle(x/size, y/size, w/size, h/size);		
		return out;
	}
	
}
